# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
#
# TODO: Separate packages into subpackages?
# TODO: Install even packages from the full tarball?
# TODO: Figure out how to run tests.
pkgname=racket
pkgver=8.11.1
pkgrel=0
pkgdesc="general purpose programming language in the Lisp-Scheme family"
url="https://racket-lang.org/"
arch="all"
license="Apache-2.0 OR MIT"
depends="
	ca-certificates
	libcrypto3
	libssl3
	"
makedepends="
	chrpath
	libffi-dev
	libucontext-dev
	lz4-dev
	ncurses-dev
	zlib-dev
	"
subpackages="
	$pkgname-dbg
	$pkgname-dev
	$pkgname-doc
	"
source="https://download.racket-lang.org/releases/$pkgver/installers/racket-minimal-$pkgver-src.tgz
	chez-musl-time_t.patch
	"
builddir="$srcdir/$pkgname-$pkgver/src"

_flags=
case "$CARCH" in
	# Racket CS (Chez Scheme backend) is not supported on these platforms,
	# so build only BC (custom backend).
	ppc64le | s390x)
		pkgdesc="Racket BC - $pkgdesc"
		license="($license) AND LGPL-3.0-or-later"
		_flags="--enable-bconly --enable-bcdefault --enable-shared"
	;;
	*)
		pkgdesc="Racket CS - $pkgdesc"
		# NOTE: --enable-shared is not supported for CS.
		_flags="--enable-csonly --enable-csdefault"
	;;
esac

prepare() {
	default_prepare
	update_config_sub || true

	# Remove bundled libffi to be sure that system-provided is used.
	rm -Rf bc/foreign/libffi
}

build() {
	export CFLAGS="${CFLAGS/-Os/-O2} -D_GNU_SOURCE"
	export CPPFLAGS="${CPPFLAGS/-Os/-O2}"
	export CXXFLAGS="${CXXFLAGS/-Os/-O2}"
	export LDFLAGS="$LDFLAGS -lucontext"

	# --enable-sharezo - this shouldn't be enabled for Racket CS because it
	#   installs arch-dependent files to /usr/share, but
	#   https://github.com/racket/racket/issues/3878#issuecomment-863358344
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--target=$CTARGET \
		--prefix=/usr \
		--sysconfdir=/etc \
		--disable-docs \
		--disable-strip \
		--enable-sharezo \
		--enable-libz \
		--enable-liblz4 \
		$_flags
	make
}

package() {
	make install DESTDIR="$pkgdir"

	rmdir "$pkgdir"/usr/share/applications
}

sha512sums="
d552ea8543a9936bb30599fe008cd8914a17b803ddf4d665fd005b943a7ad0799dceb71215bac1c5bc8fea0963a1bd04d8b2a01c3c05e9bd58ef075f9a0445d5  racket-minimal-8.11.1-src.tgz
3eacfa2fabbadc28942a77fcdc7b79a6dfbcc48d43de6e01140f1a249dc9843e2fb646631bf9b0780d982f185416d9a73a5416b75e95d2d15c6cb12070956917  chez-musl-time_t.patch
"
